package com.ruoyi.parttime.chat.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.parttime.chat.domain.ChatService;
import com.ruoyi.parttime.chat.service.IChatServiceService;
import com.ruoyi.parttime.mapper.ChatServiceMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 客服聊天Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-11
 */
@Service
public class ChatServiceServiceImpl implements IChatServiceService {
    @Autowired
    private ChatServiceMapper chatServiceMapper;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询客服聊天
     *
     * @param id 客服聊天主键
     * @return 客服聊天
     */
    @Override
    public ChatService selectChatServiceById(Long id) {
        return chatServiceMapper.selectChatServiceById(id);
    }

    /**
     * 查询客服聊天列表
     *
     * @param chatService 客服聊天
     * @return 客服聊天
     */
    @Override
    public List<ChatService> selectChatServiceList(ChatService chatService) {
        return chatServiceMapper.selectChatServiceList(chatService);
    }

    /**
     * 新增客服聊天
     *
     * @param chatService 客服聊天
     * @return 结果
     */
    @Override
    public int insertChatService(ChatService chatService) {
        return chatServiceMapper.insertChatService(chatService);
    }

    /**
     * 修改客服聊天
     *
     * @param chatService 客服聊天
     * @return 结果
     */
    @Override
    public int updateChatService(ChatService chatService) {
        return chatServiceMapper.updateChatService(chatService);
    }

    /**
     * 批量删除客服聊天
     *
     * @param ids 需要删除的客服聊天主键
     * @return 结果
     */
    @Override
    public int deleteChatServiceByIds(Long[] ids) {
        return chatServiceMapper.deleteChatServiceByIds(ids);
    }

    @Override
    public List<ChatService> selectServiceList() {

//        list.add(chatService);
        return new ArrayList<>();
    }

    @Override
    public List<ChatService> selectChatUserListById(String userId) {
        userId = SecurityUtils.getUserId().toString();
        List<ChatService> list = chatServiceMapper.selectChatUserListById(Long.getLong(userId)).stream().map(
                s -> {
                    String l = s.getSendUserAvatar();
                    s.setSendUserAvatar("http://localhost:8088" + l);
                    return s;
                }
        ).collect(Collectors.toList());
        return list;
    }

    @Override
    public SysUser selectServiceInfo() {
//        ChatService chatService = new ChatService();
//       SysUser sysUser = chatServiceMapper.selectUserById(1L);
//         chatService.setUserId(1L);
//        chatService.setUserName(sysUser.getUserName());
//        chatService.setNickName(sysUser.getNickName());
//        chatService.setServiceAvatar(sysUser.getAvatar());
        return userMapper.selectUserById(1L);
    }

    /**
     * 删除客服聊天信息
     *
     * @param id 客服聊天主键
     * @return 结果
     */
    @Override
    public int deleteChatServiceById(Long id) {
        return chatServiceMapper.deleteChatServiceById(id);
    }
}
